package com.zrc.tt;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class Utils {

    public static Boolean exists(String path) {
        File file = new File(path);
        if (file.exists()) {
            return true;
        }
        return false;
    }

    public static Boolean exists(File file) {
        if (file.exists()) {
            return true;
        }
        return false;
    }


    public static String getNameByPath(String path) {
        File file = new File(path);
        if (file.exists()) {
            return file.getName();
        }
        return null;
    }

    public static void deleteFile(File file) {
        //文件存在时，执行删除操作
        if (file.exists()) {
            if (!file.isFile()) {
                File[] files = file.listFiles();
                for (File child : files) {
                    deleteFile(child);
                }
                //删除空目录
            }
            file.delete();
        }
    }

    public static void deleteFolders(String filePath) {
        File file = new File(filePath);
        //文件存在时，执行删除操作
        if (file.exists()) {
            if (!file.isFile()) {
                File[] files = file.listFiles();
                for (File child : files) {
                    deleteFolders(child.getPath());
                }
                //删除空目录
            }
            file.delete();
        }
    }

//    /**
//     * 删除文件 和文件夹
//     *
//     * @param filePath
//     */
//    public static void deleteFolders(String filePath) {
//        if (!new File(filePath).exists()) {
//            System.out.println("你要删除的文件不存在");
//            return;
//        }
//        Path path = Paths.get(filePath);
//        try {
//            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
//                @Override
//                public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException {
//                    Files.delete(file);
//                    return FileVisitResult.CONTINUE;
//                }
//
//                @Override
//                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
//                    Files.delete(dir);
//                    return FileVisitResult.CONTINUE;
//                }
//            });
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    public static String linkPath(String basePath, String... dentrys) {
        StringBuilder stringBuilder = new StringBuilder(basePath);
        String[] var4 = dentrys;
        int var5 = dentrys.length;

        for (int var6 = 0; var6 < var5; ++var6) {
            String dentry = var4[var6];
            stringBuilder.append(File.separator);
            stringBuilder.append(dentry);
        }
        return stringBuilder.toString();
    }

    /**
     * 读取文件
     *
     * @param file
     * @return
     * @throws Exception
     */
    public static byte[] getBytes(File file) throws Exception {
        RandomAccessFile r = new RandomAccessFile(file, "r");
        byte[] buffer = new byte[(int) r.length()];
        r.readFully(buffer);
        r.close();
        return buffer;
    }
}

